from typing import Union

from fastapi import APIRouter

getDemo = APIRouter()
"""
路径参数
"""
@getDemo.get("/user1/{user_id}")
def get_user1(user_id):
    print(user_id, type(user_id))
    return {"user_id": user_id}

@getDemo.get("/user2/{user_id}")
def get_user2(user_id: int):
    print(user_id, type(user_id))
    return {"user_id": user_id}

@getDemo.get("/user3/{username}")
async def read_user(username: str):
    return {"username": username}

"""
查询参数
"""
@getDemo.get("/user4")
async def search_jobs(kd: str,
                      city: Union[str, None] = None,
                      username: Union[str, None] = None):  # 有默认值即可选，否则必选
    if city or username:
        return {"kd": kd, "city": city, "username": username}
    return {"kd": kd}